﻿using System;
using System.Windows.Forms;
using Live.Refresh.Components;
using System.Linq;

namespace Live.Refresh.App
{
    public partial class MainForm : Form
    {
        private SignalServer server;
        public MainForm()
        {
            InitializeComponent();
            BootStrapper.Bootstrap();
            FillList();
        }

        private void FillList()
        {
            foreach (var comp in Resolver.Resolve<Watchable>().OrderBy(c=> c.Order))
            {
                var tb = new TabPage(comp.Name) {Dock = DockStyle.Fill, Tag = comp};
                tabControl1.TabPages.Add(tb);
            }
            tabControl1_SelectedIndexChanged(null, null);
        }
       
        private void SignalServer()
        {
            server = new SignalServer(System.Configuration.ConfigurationManager.AppSettings["server"]);
            server.MapHubs();
            server.Start();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
             	if 	(	tabControl1.	TabIndex 	!= 	-1)
                {
                    var tb = tabControl1.TabPages[tabControl1.SelectedIndex];
                    PropertyGrid gr;
                    if (tb.Controls.Count > 0)
                    {
                        gr = (PropertyGrid)tb.Controls[0];
                    }
                    else
                    {
                        gr = new PropertyGrid {Dock = DockStyle.Fill};
                        tb.Controls.Add(gr);
                    }
                    gr.SelectedObject = ((Watchable)tb.Tag).Configurable;
                    btnStart.Enabled = !((Watchable) tb.Tag).IsStarted;
                }

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (tabControl1.TabIndex != -1)
            {
                var tb = tabControl1.TabPages[tabControl1.SelectedIndex];
                var watchable = ((Watchable)tb.Tag);
                if (watchable.Name != "DirWatcher")
                {
                    MessageBox.Show("Can't start this component (not implemented yet) please use DirWatcher!");
                    return;
                }
                if(server == null)
                {
                    SignalServer();
                }                

                watchable.StartNotify();
                btnStart.Enabled = false;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            var tb = tabControl1.TabPages[tabControl1.SelectedIndex];
            var watchable = ((Watchable)tb.Tag);
            watchable.Stop();
            btnStart.Enabled = true;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (server != null)
            {
                server.Stop();
            }
        }
    }
}
